﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    class DAO_v2
    {
        public string StringConexion()
        {
          //return @"Data Source=JUAN-PC\SQLEXPRESS;Initial Catalog=Nueva_PAVII;Integrated Security=True";
          return "Data Source=EZEQUIEL-PC\\SQLEXPRESS;Initial Catalog=Nueva_PAVII;Integrated Security=True";
           // return "Data Source=ESCRITORIO\\SQLEXPRESS;Initial Catalog=Nueva_PAVII;Integrated Security=True";
        }
        SqlConnectionStringBuilder csb;

        public DAO_v2(String dataSource, String initialCatalog, String userID, String password)
        {
            csb = new SqlConnectionStringBuilder();
            csb.DataSource = dataSource;
            csb.InitialCatalog = initialCatalog;
            csb.UserID = userID;
            csb.Password = password;
        }

        public DAO_v2()
        {

        }

        public String CadenaConexion()
        {
            return csb.ConnectionString;
        }

        static public string MiCadenaConexion()
        {
            //Pone tu conexion aca
            return "Data Source=EZEQUIEL-PC\\SQLEXPRESS;Initial Catalog=Nueva_PAVII;Integrated Security=True";
            //return "Data Source=ESCRITORIO\\SQLEXPRESS;Initial Catalog=Nueva_PAVII;Integrated Security=True";
            //return @"Data Source=JUAN-PC\SQLEXPRESS;Initial Catalog=Nueva_PAVII;Integrated Security=True";
        }

        public SqlCommand Grabar(String dbTabla, List<String> parametros, List<Object> datos)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            String comando = "INSERT INTO ";
            comando += dbTabla + " (";
            for (int i = 0; i < parametros.Count(); i++)
            {
                comando += parametros[i];
                if (i != parametros.Count() - 1)
                {
                    comando += ", ";
                }
                else
                {
                    comando += ") ";
                }
            }
            comando += "VALUES (";
            for (int i = 0; i < datos.Count(); i++)
            {
                comando += "@" + parametros[i];
                if (i != datos.Count() - 1)
                {
                    comando += ", ";
                }
                else
                {
                    comando += ") ";
                }
                cmd.Parameters.AddWithValue("@" + parametros[i], datos[i]);
            }
            //cmd.CommandText = comando + "; SELECT CAST(scope_identity() AS int)";
            cmd.CommandText = comando + ";SELECT @@IDENTITY AS 'Identity'";
            return cmd;
        }

        public SqlCommand Buscar(String dbTabla, List<String> columnas, List<String> parametros, List<Object> datos)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            String comando = "SELECT ";
            for (int i = 0; i < columnas.Count(); i++)
            {
                comando += columnas[i];
                if (i != columnas.Count() - 1)
                {
                    comando += ", ";
                }
                else
                {
                    comando += " ";
                }
            }
            comando += "FROM ";
            comando += dbTabla + " ";
            comando += "WHERE 1 = 1";
            for (int i = 0; i < parametros.Count(); i++)
            {
                comando += " AND " + parametros[i] + " = " + "@" + parametros[i] + " ";
                cmd.Parameters.AddWithValue("@" + parametros[i], datos[i]);
            }
            cmd.CommandText = comando;
            return cmd;
        }

        public SqlCommand Actualizar(String dbTabla, List<String> parametrosSet, List<Object> datosSet, List<String> parametrosWhere, List<Object> datosWhere)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            String comando = "UPDATE ";
            comando += dbTabla + " ";
            comando += "SET ";
            for (int i = 0; i < parametrosSet.Count(); i++)
            {
                comando += parametrosSet[i] + " = " + "@" + parametrosSet[i];
                if (i != parametrosSet.Count() - 1)
                {
                    comando += ", ";
                }
                else
                {
                    comando += " ";
                }
                cmd.Parameters.AddWithValue(parametrosSet[i], datosSet[i]);
            }
            comando += "WHERE ";
            for (int i = 0; i < parametrosWhere.Count(); i++)
            {
                comando += parametrosWhere[i] + " = " + "@" + parametrosWhere[i] + " ";

                if (i != parametrosWhere.Count() - 1)
                {
                    comando += "AND ";
                }
                cmd.Parameters.AddWithValue("@" + parametrosWhere[i], datosWhere[i]);
            }
         
            cmd.CommandText = comando + " ;SELECT @@IDENTITY AS 'Identity'";
            return cmd;
        }

        public SqlCommand Eliminar(String dbTabla, List<String> parametros, List<Object> datos)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;

            String comando = "DELETE FROM ";
            comando += dbTabla + " ";
            comando += "WHERE ";
            for (int i = 0; i < parametros.Count(); i++)
            {
                comando += parametros[i] + " = " + "@" + parametros[i];
                if (i != parametros.Count() - 1)
                {
                    comando += "AND ";
                }
                else
                {
                    comando += " ";
                }
                cmd.Parameters.AddWithValue("@" + parametros[i], datos[i]);
            }
            cmd.CommandText = comando;
            return cmd;
        }

        public void CerrarConexion(SqlConnection con)
        {
            if (con.State == System.Data.ConnectionState.Open)
            {
                con.Close();
            }
        }


        public SqlCommand CargarCombo(String dbTabla, List<String> columnas, String condicionExtra)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            String comando = "SELECT ";
            for (int i = 0; i < columnas.Count(); i++)
            {
                comando += columnas[i];
                if (i != columnas.Count() - 1)
                {
                    comando += ", ";
                }
                else
                {
                    comando += " ";
                }
            }
            comando += "FROM ";
            comando += dbTabla + " ";
            comando += condicionExtra;
            cmd.CommandText = comando;
            return cmd;
        }

        public SqlCommand BuscarVariasTablasSinFiltro(List<String> tablas, List<String> columnas, List<String> fk, List<String> pk)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            String comando = "SELECT ";
            for (int i = 0; i < columnas.Count(); i++)
            {
                comando += columnas[i];
                if (i != columnas.Count() - 1)
                {
                    comando += ", ";
                }
                else
                {
                    comando += " ";
                }
            }
            comando += "FROM ";
            for (int i = 0; i < tablas.Count(); i++)
            {
                comando += tablas[i];
                if (i != tablas.Count() - 1)
                {
                    comando += ", ";
                }
                else
                {
                    comando += " ";
                }
            }
            comando += "WHERE 1 = 1";
            for (int i = 0; i < fk.Count(); i++)
            {
                comando += " AND " + fk[i] + " = " + pk[i] + " ";
            }
            cmd.CommandText = comando;
            return cmd;
        }

        public SqlCommand BuscarVariasTablasConFiltro(List<String> tablas, List<String> columnas, List<String> fk, List<String> pk, List<String> parametros, List<Object> datos)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            String comando = "SELECT ";
            for (int i = 0; i < columnas.Count(); i++)
            {
                comando += columnas[i];
                if (i != columnas.Count() - 1)
                {
                    comando += ", ";
                }
                else
                {
                    comando += " ";
                }
            }
            comando += "FROM ";
            for (int i = 0; i < tablas.Count(); i++)
            {
                comando += tablas[i];
                if (i != tablas.Count() - 1)
                {
                    comando += ", ";
                }
                else
                {
                    comando += " ";
                }
            }
            comando += "WHERE 1 = 1";
            for (int i = 0; i < fk.Count(); i++)
            {
                comando += " AND " + fk[i] + " = " + pk[i] + " ";
            }
            for (int i = 0; i < parametros.Count(); i++)
            {
                comando += " AND " + parametros[i] + " = " + "@" + parametros[i] + " ";
                cmd.Parameters.AddWithValue("@"+parametros[i],datos[i]);
            }
            cmd.CommandText = comando;
            return cmd;
        }
    }
}

